﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using System.Xml;
using Microsoft.Xna.Framework;
namespace MenuGame2DXNA
{
    public class GroupTextEntity : VisibleGameEntity
    {
      
        public void SetScale(float scale)
        {

            for (int i = 0; i < nObjectVisible; i++)
            {
                ObjectVisible[i].Scale = scale;
            }
            
        }
        public GroupTextEntity(ContentManager contentManager, String FileNameXml)
        {
            PositionStart = new Vector2(0, 0);
          
            ObjectVisible = new List<TextEntity>();
            // Load content in xml file
            XmlDocument Grouptextxml = new XmlDocument();
            Grouptextxml.Load(FileNameXml);
            XmlElement root = (XmlElement)Grouptextxml.SelectSingleNode("GroupText");
            foreach (XmlElement xe in root.ChildNodes)
            {

                XmlElement textxml = (XmlElement)xe.SelectSingleNode("Text");
                XmlElement fontxml = (XmlElement)xe.SelectSingleNode("NameFont");
                XmlElement Scalexml = (XmlElement)xe.SelectSingleNode("Scale");
                XmlElement Alignxml = (XmlElement)xe.SelectSingleNode("Align");
                XmlElement Colorxml = (XmlElement)xe.SelectSingleNode("Color");
                XmlElement Boundxml = (XmlElement)xe.SelectSingleNode("Bound");

                float[] color = new float[4];

                color[0] = float.Parse(Colorxml.GetAttribute("r"));
                color[1] = float.Parse(Colorxml.GetAttribute("g"));
                color[2] = float.Parse(Colorxml.GetAttribute("b"));
                color[3] = float.Parse(Colorxml.GetAttribute("a"));



                int x = int.Parse(Boundxml.GetAttribute("x"));
                int y = int.Parse(Boundxml.GetAttribute("y"));
                int w = int.Parse(Boundxml.GetAttribute("w"));
                int h = int.Parse(Boundxml.GetAttribute("h"));

                TextEntity tempTextEntity = new TextEntity(contentManager,
                    fontxml.InnerText,
                    new Vector2(w, h),
                    new Vector2(x,y),
                    textxml.InnerText,
                    (MenuGame2DXNA.TextEntity.Alignment)int.Parse(Alignxml.InnerText),
                    float.Parse(Scalexml.InnerText)
                    );
                tempTextEntity.ColorText = new Color(color[0], color[1], color[2], color[3]);             
                tempTextEntity.NameEntity = xe.GetAttribute("name");
                ObjectVisible.Add(tempTextEntity);
                nObjectVisible = ObjectVisible.Count;


            }
        }
        public GroupTextEntity(ContentManager content, int Number=1)
         {
             PositionStart = new Vector2(0, 0);
             ObjectVisible = new List<TextEntity>();
            for (int i = 0 ;i < Number;i++)
            {
                ObjectVisible.Add(new TextEntity(content));
            }
            nObjectVisible = Number;
        }

        public TextEntity this[int Index]    // Indexer declaration
        {
            get
            {
                return ObjectVisible[Index];
            }
           
        }
        public TextEntity this[String nameEntity]    // Indexer declaration
        {
            get
            {
                foreach (TextEntity te in ObjectVisible)
                {
                    if (te.NameEntity == nameEntity)
                        return te;
                }
                return null;
            }

        }
        public override void Update(GameTime gameTime, Color color)
        {
            for (int i = 0; i < nObjectVisible; i++)
            {
                ObjectVisible[i].Update(gameTime, color);
            }
        }
        public void UpdateRealPosition(Vector2 positon)
        {
             
             for (int i = 0; i < nObjectVisible; i++)
            {
                ObjectVisible[i].PositionStart += (positon - PositionStart);
            }
            PositionStart = positon;
        }
        public override void UpdatePositon()
        {
            for (int i = 0; i < nObjectVisible; i++)
            {
                ObjectVisible[i].TopLeft = TopLeft;
            }
        }

    }
}
